package com.chinasi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinasi.dao.DictDataDao;
import com.chinasi.dao.SysDictDao;
import com.chinasi.model.DictData;
import com.chinasi.model.SysDict;
import com.chinasi.service.SysDictService;

@Service
public class SysDictServiceImpl implements SysDictService {

	/**
	 * SysDictDAO
	 */
	@Autowired
	private SysDictDao sysDictDao;
	
	@Autowired
	private DictDataDao dictDataDao;

	@Override
	public SysDict findByCode(String code) {
		SysDict result = null;
		SysDict searchBean = new SysDict();
		searchBean.setDictCode(code);
		List<SysDict> list = sysDictDao.findByCondition(searchBean);
		if(list != null && list.size() > 0){
			result = list.get(0);
			loadDictData(result);
		}
		return result;
	}

	private void loadDictData(SysDict dict) {
		DictData searchBean = new DictData();
		searchBean.setDictoid(dict.getDictoid());
		dict.setDictDataList(dictDataDao.findByCondition(searchBean));
	}

	@Override
	public List<SysDict> findAll() {
		List<SysDict> result = null;
		result = sysDictDao.findByCondition(new SysDict());
		if(result != null && result.size() > 0){
			for(SysDict item : result){
				loadDictData(item);
			}
		}
		return result;
	}
	
}
